package com.h3bpm.kumhosunny.crm.controller.customercontact;/**
 * @Auther: Administrator
 * @Date: 2019/10/17 16:44
 * @Description:
 */

import OThinker.H3.Controller.ControllerBase;
import com.alibaba.fastjson.JSONObject;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import data.DataRow;
import data.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:
 * @Description: 描述
 *
 * @author WJQ
 * @version V1.0
 * @Date
 */
@Controller
@RequestMapping("/Portal/CRM/CustomerProfileCar")
public class CustomerProfileCarController extends ControllerBase {
    private static Logger log = LoggerFactory.getLogger(CustomerProfileCarController.class);
    private  final String ONE="1";

    private  final String TOW="2";
    @Override
    public String getFunctionCode() {
        return null;
    }
    @RequestMapping("/getCarList")
    @ResponseBody
    public JSONObject getBoList(String clientData, String state ){
        log.info("移动端汽车客户档案接口==================clientData="+clientData);
        JSONObject result= new JSONObject();
        String userId =  getUserValidator().getUser().getObjectID();
        String parentId =  getUserValidator().getUser().getManagerID();
        try {
            String sql = "SELECT  b.`Name` AS man,c.ObjectID,c.customerName,c.customerStatus,c.mainProducts FROM ( " +
                    "SELECT CreatedTime,ObjectID,customerName,customerStatus,mainProducts,salesman,CreatedByParentId FROM i_customerprofile WHERE sheetYype='汽车' " +
                    ") c " +
                    "LEFT JOIN  ot_user b ON c.salesman = b.ObjectID where 1=1 ORDER BY c.CreatedTime DESC";
            if(clientData != null &&  !"".equals(clientData)){
                sql="SELECT  b.`Name` AS man,c.ObjectID,c.customerName,c.customerStatus,c.mainProducts FROM ( " +
                        "SELECT CreatedTime,ObjectID,customerName,customerStatus,mainProducts,salesman,CreatedByParentId FROM i_customerprofile WHERE sheetYype='汽车' " +
                        "AND customerName like '%"+clientData+"%' OR customerStatus like '%"+clientData+"%' or mainProducts like '%"+clientData+"%' OR salesman LIKE '%"+clientData+"%' " +
                        ") c  LEFT JOIN  ot_user b ON c.salesman = b.ObjectID where 1=1 ";
            }
            DataTable table = null;

            if(ONE.equals(state)){
                sql+=" and c.salesman = '"+userId+"'"+" ORDER BY c.CreatedTime DESC";
            }
            //按组织权限查询当前组织下的联系人
            if(TOW.equals(state)){
                sql+=" and c.OwnerId = '"+parentId+"'" +" ORDER BY c.CreatedTime DESC";
            }

            table = SqlUtil.doQuery(sql);

            List<Map<String ,String >> list =new ArrayList<Map<String ,String > >();
            if(table!=null && table.getEntityRows()!=null && table.getEntityRows().size()>0){
                for( DataRow dataRow :  table.getEntityRows()) {
                    Map<String, String> customerMap = new HashMap<String, String>(16);
                    String objectId=dataRow.getString("ObjectID");
                    String customerName=dataRow.getString("customerName");
                    String customerStatus=dataRow.getString("customerStatus");
                    String mainProducts=dataRow.getString("mainProducts");
                    String exclusiveSalesman=dataRow.getString("man");
                    customerMap.put("customerName",customerName);
                    customerMap.put("customerStatus",customerStatus);
                    customerMap.put("mainProducts",mainProducts);
                    customerMap.put("ObjectId",objectId);
                    customerMap.put("salesman",exclusiveSalesman);
                    list.add(customerMap);
                }
            }
            result.put("data",list);
            result.put("status","200");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/testCustomerAndCode")
    @ResponseBody
    public JSONObject testCustomerAndCode(String customerName, String customerCode ){
        log.info("客户档案接口=======customerName="+customerName+"=======customerCode="+customerCode);
        JSONObject result= new JSONObject();
        String sql = "SELECT customerName FROM i_customerprofile WHERE customerName='"+customerName+"'";
        String sql2 = "SELECT customerCode FROM i_customerprofile WHERE customerCode='"+customerCode+"'";
        DataTable table = null;
        try {
        table = SqlUtil.doQuery(sql);
            if(table!=null && table.getEntityRows()!=null && table.getEntityRows().size()>0){
                result.put("data","客户名称已存在！");
                result.put("status","200");
                return  result;
            }
            table = SqlUtil.doQuery(sql2);
            if(table!=null && table.getEntityRows()!=null && table.getEntityRows().size()>0){
                result.put("data","客户编码名称已存在！");
                result.put("status","201");
                return  result;
            }
            result.put("data","可以添加");
            result.put("status","202");
            return  result;
          } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping("/skipSoloCarList")
    public String skipSoloList(){

        return "redirect:/Portal/Mobile/kumhosunny/customerProfileCar.html?state=1";
    }


    @RequestMapping("/skipUnitCarList")
    public String skipUnitLinkmanList(){

        return "redirect:/Portal/Mobile/kumhosunny/customerProfileCar.html?state=2";
    }


}
